<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

switch ($op) {
	case "ext": // unregistered user
		$t = new temporal;
		$t->id=util::encrypt($param['email'].CODE_REQUEST.$param['teamid']);
		$t->options=CODE_REQUEST;
		$t->extra=$param['email'];
		$t->expire=util::now();

		$us = new user;
		if ($param['teamid']=="ADMIN") {
			$us->groupid = GROUP_ADMIN;
			$ulist = $loader->loadListBy($us,$us->listByGroup());
			$t->teamid = "0";	
		} else {
			$us->groupid = GROUP_COORD;
			$us->teamid = $param['teamid'];
			$ulist = $loader->loadListBy($us,$us->listByFilters());
			$t->teamid = $param['teamid'];
			
			$team = new team;
			$team->id=$t->teamid;
			$loader->get($team);
			
			$ts = new teamsetting;
			$ts->teamid = $t->teamid;
			$ts->id=AUTO_REPLY;
			if ($loader->get($ts)) {
				$ts->id=AUTO_REPLY_MSG;
				if ($loader->get($ts)) {
					mailAutoReply($t->extra,$ts->value,$team->lang);
				}
			}
			$ts->id=AUTO_SEND;
			if ($loader->get($ts)) {
				$t1 = new temporal;
				$t1->id = util::encrypt($t->id);
				$loader->delete($t1);
				
				$t1->options = CODE_USER_REG;
				$t1->extra=$t->extra;
				$t1->teamid = $t->teamid;
				$t1->filler=$t->id;
				$loader->save($t1);
				
				mailInviteUser($t1->extra,$team->name,$t1->id,$t1->expire,$team->lang);			
			}
		}
		$loader->save($t);
		
		mailRequestAccount($ulist,$param['email'],$param['message']);

		success(s("Your message was sent. Thank you."));
		break;
	case "user":
		$ids = stripslashes($param['ids']);
		$idlist = json_decode($ids);
		if (String::isEmpty($CURRENT_USER->name)) {
			$from = $CURRENT_USER->userid;	
		} else {
			$from = $CURRENT_USER->name . " (".$CURRENT_USER->userid.")";
		}		
		foreach ($idlist as $id) {
			$u = new user;
			$u->userid=$id;
			if ($loader->get($u)) {
				$text = String::format(s("Message from {0}:\n\n",$u->lang),$from).$param['text'];
				sendMail($u->mail,$param['subject'],$text,$CURRENT_USER->mail,String::coallesce($CURRENT_USER->name,$CURRENT_USER->userid));	
			}
		}
		success(s("Message sent."));		
	default:
		break;
}

?>